package com.kstudio.videocapture.util.bitmapload;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.Bitmap;

public class BitmapDoubleCache<E> {
    private static final int THREAD_STATE_READY         = 0x0000;
    private static final int THREAD_STATE_RUNNING       = 0x0001;

    private BitmapLruCacheMemory mMemoryCache;
    private BitmapLruCacheDisk   mDiskCache;

    public LinkedList<Item> mLoadList;

    private LoadBitmapTask  mLoadBitmapTask;
    private int             mLoadBitmapTaskState;

    public BitmapDoubleCache(Context context, String uniqueName, int MemoryCacheSize, int diskCacheSize) {
        mMemoryCache = new BitmapLruCacheMemory(MemoryCacheSize);
        mDiskCache = new BitmapLruCacheDisk(context, uniqueName, diskCacheSize);

        mLoadList = new LinkedList<Item>();

        mLoadBitmapTaskState = THREAD_STATE_READY;
    }

    public void stop() {
        if(mLoadBitmapTaskState == THREAD_STATE_RUNNING) {
            mLoadBitmapTask.stopThread();
            mLoadBitmapTaskState = THREAD_STATE_READY;
        }
    }

    public Bitmap get(BitmapDoubleCacheParam param, E obj) {
        Bitmap bitmap = null;
        String key = param.getKey();

        bitmap = mMemoryCache.get(key);

        if(bitmap == null) {
            synchronized (mLoadList) {
                boolean skip = false;

                for(Item item : mLoadList) {
                    if(item.param.getKey().compareTo(key) == 0) {
                        skip = true;
                        break;
                    }
                }

                if(!skip) {
                    Item item = new Item(param, obj);
                    mLoadList.add(item);
                }
            }

            if(mLoadBitmapTaskState == THREAD_STATE_READY) {
                mLoadBitmapTaskState = THREAD_STATE_RUNNING;
                mLoadBitmapTask = new LoadBitmapTask();
                mLoadBitmapTask.start();
            }
        }

        return bitmap;
    }

    public void add(Item item) {

    }

    private class Item {
        BitmapDoubleCacheParam param;
        E      obj;

        Item(BitmapDoubleCacheParam param, E obj) {
            this.param = param;
            this.obj = obj;
        }
    }

    private class LoadBitmapTask extends Thread {
        public boolean running;

        public void stopThread() {
            running = false;
        }

        @Override
        public void run() {
            running = true;

            Item item = null;
            Bitmap bm = null;

            while(running) {
                synchronized (mLoadList) {
                    if(mLoadList.size() > 0) {
                        item = mLoadList.remove();
                    } else {
                        running = false;
                        mLoadBitmapTaskState = THREAD_STATE_READY;
                        break;
                    }
                }

                String key = item.param.getKey();
                bm = mDiskCache.get(key);

                if(bm == null) {
                    bm = loadBitmap(item.param.getPath());
                }

                if(bm != null) {
                    mDiskCache.put(key, bm);
                    mMemoryCache.put(key, bm);

                    onLoadBitmapUpdateFromCache(item.obj, bm);
                }
            }
        }
    }

    protected void onLoadBitmapUpdateFromCache(E obj, Bitmap bm) {

    }

    protected Bitmap loadBitmap(String filename) {
        return null;
    }
}
